ARG BASE_IMAGE=pytorch/pytorch:2.5.1-cuda11.8-cudnn9-devel
ARG DEBIAN_FRONTEND=noninteractive
FROM ${BASE_IMAGE}

ENV ALGO_NAME=grounding_dino
ENV APP_ROOT=/opt/${ALGO_NAME}
ENV CUDA_HOME=/usr/local/cuda \
     TORCH_CUDA_ARCH_LIST="6.0 6.1 7.0 7.5 8.0 8.6+PTX" \
     SETUPTOOLS_USE_DISTUTILS=stdlib
ENV DEBIAN_FRONTEND=noninteractive

# Install libraries in the brand new image. 
# 预先配置时区
RUN ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" > /etc/timezone

# 替换为国内镜像源
RUN apt-get -y update && \
    apt-get install -y --no-install-recommends \
         wget \
         build-essential \
         git \
         python3-opencv \
         ca-certificates && \
    rm -rf /var/lib/apt/lists/*

# 升级 pip 和 setuptools
RUN pip install --upgrade pip setuptools --index-url https://pypi.tuna.tsinghua.edu.cn/simple

# 创建应用目录
RUN mkdir -p ${APP_ROOT}

# 复制 grounding_dino 代码到容器中
COPY ./models/${ALGO_NAME} ${APP_ROOT}/agents/models/${ALGO_NAME}

# 复制 mq 代码到容器中
COPY ./mq ${APP_ROOT}/agents/mq

# 设置工作目录
WORKDIR ${APP_ROOT}/agents/models/${ALGO_NAME}

# 安装 Python 依赖，使用清华源
RUN pip install -r requirements.txt --no-deps --no-build-isolation -v --index-url https://pypi.tuna.tsinghua.edu.cn/simple

# 下载github GroundingDINO仓库
RUN git clone https://github.com/IDEA-Research/GroundingDINO.git
RUN cd GroundingDINO/ && python -m pip install .

# 下载模型参数
RUN mkdir -p ${APP_ROOT}/checkpoints
ADD https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth ${APP_ROOT}/checkpoints/groundingdino_swint_ogc.pth

# 下载grouding_dino的其他模型文件
RUN python3 run.py --build

CMD [ "python", "run.py" ]